<template>
  <div class="login container">
    <Header></Header>
    <section>
      <form action="">
        <div class="login-tel">
          <input type="tel" v-model="userTel" placeholder="请输入手机号..." />
        </div>
        <div class="login-code">
          <input type="tel" v-model="userCode" placeholder="请输入验证码" />
          <button 
            :disabled="disabled"
            @click="sendCode"
            >{{codeMsg}}</button>
        </div>
        <div class="login-btn" @click="login">登 录</div>
        <div class="tab">
            <span @click="goUserLogin">密码登录</span>
            <span @click="goRegister">快速注册</span>
        </div>
      </form>

    </section>
    <Tabbar></Tabbar>
  </div>
</template>

<script>
import Header from "@/views/login/Header.vue";
import Tabbar from "@/components/common/Tabbar.vue";
import { Toast } from 'mint-ui';
import http from '@/common/api/request';
import {mapMutations} from 'vuex';
export default {
  data () {
    return {
      disabled:false,
      userCode:'',
      userTel:'',
      //验证规则
      rules: {
        userTel: {
          rule: /^1[23456789]\d{9}$/,
          msg: "手机号不能为空,并且是11位",
        }
      },
      codeTime:30,
      codeMsg:'获取验证码',
      code:''
    }
  },
  components: {
    Header,
    Tabbar,
  },
  methods:{
    ...mapMutations(['USER_LOGIN']),
    sendCode(){
      if (!this.validate("userTel")) return;

      //请求短信验证码接口
      http.$axios({
        url:'/api/code',
        method:'POST',
        data:{
            phone:this.userTel,
        }
      }).then(res=>{
        if (res.success){
          this.code = res.data;
        }
      })

      this.disabled = true;

      let timer = setInterval(() => {
        --this.codeTime;
        this.codeMsg = `重新获取 ${this.codeTime}`;
      }, 1000);
      setInterval(() => {
        clearInterval(timer);
        this.codeTime = 30;
        this.codeMsg = '获取验证码';
        this.disabled = false;
      }, 30000);
    },
    goUserLogin() {
        this.$router.push('/userLogin');
    },
    goRegister() {
        this.$router.push('/register');
    },
        //验证信息提示
    validate(key) {
        let bool = true;
        if (!this.rules[key].rule.test(this[key])){
            Toast(this.rules[key].msg);
            bool = false;
            return false
        }
        return bool
    },
    //点击登录
    login() {
      if (this.code == this.userCode){
        http.$axios({
          url:'/api/addUser',
          method:'POST',
          data:{
              phone:this.userTel,
          }
        }).then(res=>{
          if (!res.success) return;
          //登陆成功 ==》跳转页面,存储用户信息
          this.USER_LOGIN(res.data);

          this.$router.replace('/my');
          })
      }
    }
  }
};
</script>

<style lang="scss" scoped>
section {
    display: flex;
    flex-direction: column;
    align-items: center;
    font-size: .4rem;
    background-color: #f5f5f5;
    div {
        margin: .2667rem 0;
        width: 8.9333rem;
        height: 1.1733rem;
    }
    input {
        box-sizing: border-box;
        padding: 0 .2667rem;
        line-height: 1.1733rem;
        background-color: #fff;
        border: 1px solid #ccc;
        border-radius: .16rem;
    }
    .login-tel {
        margin-top: .8rem;
        input {
            width: 100%;
        }
    }
    .login-code {
        display: flex;
        input {
            flex: 1;
        }
        button {
            padding: 0 .5333rem;
            line-height: 1.1733rem;
            color: #fff;
            background-color: #b0352f;
            border: none;
            border-radius: .16rem;
        }
    }
    .login-btn{
        line-height: 1.1733rem;
        text-align: center;
        color: #fff;
        background-color: #b0352f;
        border-radius: .16rem;
    }
    .tab {
        display: flex;
        justify-content: space-between;
        align-items: center;
        span {
            font-size: .3733rem;
        }
    }
}
</style>
